import common from './public.js'
$(function () {
	function paging(pageNow, pageSize, fuzzyQuery, department){
		window.location.href = common.baseUrl + "/user/list.do?pageNow=" + pageNow + "&pageSize=" + pageSize + "&fuzzyQuery=" + fuzzyQuery + "&department=" + department;
	}
	$("#user-search").keydown(function(event){
		event = event || window.event
		if(event.keyCode == 13){
			paging(1, $("#user-set-page-size option:selected").val(), $("#user-search").val(), $("#user-select-departments option:selected").val())
		}
	})
	$("#user-search-icon").click(function(){
		paging(1, $("#user-set-page-size option:selected").val(), $("#user-search").val(), $("#user-select-departments option:selected").val())
	})
	$("#user-set-page-size").change(function (){
		paging(1, $("#user-set-page-size option:selected").val(), $("#user-search").val(), $("#user-select-departments option:selected").val())
	})
	$("#user-select-departments").change(function (){
		paging(1, $("#user-set-page-size option:selected").val(), $("#user-search").val(), $("#user-select-departments option:selected").val())
	})
	$("#user-btn-batch-deletion").click(function(){
		var codes = []
		$('input[name="code"]:checked').each(function(index, element) {
			//追加到数组中
			codes.push($(this).val());
		});
		common.post("/user/deleted.do", {codes: codes.join(",")}, function(res){
			if(res.code === 200){
				 common.promptSuccess(res.msg)
				 paging(1, $("#user-set-page-size option:selected").val(), $("#user-search").val(), $("#user-select-departments option:selected").val())
				 // window.location = common.baseUrl + "/oneself.do"
			}else{
				 common.promptError(res.msg)
			}
		})
	})
	$("#user-pre-page").click(function(){
		paging(parseInt($("#user-page-now").text()) - 1, $("#user-set-page-size option:selected").val(), $("#user-search").val(), $("#user-select-departments option:selected").val())
	})
	$("#user-next-page").click(function(){
		paging(parseInt($("#user-page-now").text()) + 1, $("#user-set-page-size option:selected").val(), $("#user-search").val(), $("#user-select-departments option:selected").val())
	})
	$("#user-btn-add").click(function(){
		common.get("/department/getall.do",{},function (res){
			if(res.code === 200){
				 let data = res.data;
				 for (var i = 0; i < data.length; i++) {
					 var option = document.createElement("option")
					 option.setAttribute("value", data[i].id)
					 option.text = data[i].name
				 	$("#user-add-department").append(option)
				 }
			}else{
				 common.promptError(res.msg)
			}
		})
	})
	$("#user-add-submit-btn").click(function (){
		common.post("/user/add.do",{
			name: $("#user-add-name").val(),
			gender: $("#user-add-gender").val(),
			birthDate: $("#user-add-birthDate").val(),
			jobTitle: $("#user-add-jobTitle").val(),
			education: $("#user-add-education").val(),
			degree: $("#user-add-degree").val(),
			qq: $("#user-add-QQ").val(),
			email: $("#user-add-email").val(),
			introduction: $("#user-add-introduction").val(),
			departmentId: $("#user-add-department option:selected").val()
		},function (res){
			if(res.code === 200){
				common.promptSuccess(res.msg)
				paging(1, $("#user-set-page-size option:selected").val(), $("#user-search").val(), $("#user-select-departments option:selected").val())
			}else{
				 common.promptError(res.msg)
			}
		})
	})
	$("#user-update-submit-btn").click(function(){
		common.post("/user/update.do",{
			code: $("#user-update-code").val(),
			name: $("#user-update-name").val(),
			gender: $("#user-update-gender").val(),
			birthDate: $("#user-update-birthDate").val(),
			jobTitle: $("#user-update-jobTitle").val(),
			education: $("#user-update-education").val(),
			degree: $("#user-update-degree").val(),
			qq: $("#user-update-QQ").val(),
			email: $("#user-update-email").val(),
			introduction: $("#user-update-introduction").val(),
			password: $("#user-update-password").val(),
			departmentId: $("#user-update-department option:selected").val()
		},function (res){
			console.log(res);
			console.log(res.code === 200);
			if(res.code === 200){
				common.promptSuccess(res.msg)
				paging(1, $("#user-set-page-size option:selected").val(), $("#user-search").val(), $("#user-select-departments option:selected").val())
			}else{
				 common.promptError(res.msg)
			}
		})
	})
})